<?php

/**
 * This is the model class for table "{{menu_type}}".
 *
 * The followings are the available columns in table '{{menu_type}}':
 * @property integer $id
 * @property string $name
 * @property string $description
 * @property integer $status
 * @property string $created_by
 * @property string $create_date
 * @property string $updated_by
 * @property string $update_date
 */
class MenuType extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return MenuType the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{menu_type}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
	// NOTE: you should only define rules for those attributes that
	// will receive user inputs.
	return array(
	    array('name', 'required', 'message' => t('MSG_REQUIRED', 'messages')),
	    //array('status', 'numerical', 'integerOnly'=>true),
	    array('name', 'length', 'max' => 100, 'tooLong' => t('MSG_STRING_TOO_LONG', 'messages'), 'on' => 'create, update'),
	    array('description', 'length', 'max' => 500, 'tooLong' => t('MSG_STRING_TOO_LONG', 'messages'), 'on' => 'create, update'),
	    array('created_by, updated_by', 'length', 'max' => 255),
	    // The following rule is used by search().
	    // Please remove those attributes that should not be searched.
	    array('id, name, description, status, created_by, create_date, updated_by, update_date', 'safe', 'on' => 'search'),
	);
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'id' => t('LBL_ID', 'menuType'),
	    'name' => t('LBL_NAME', 'menuType'),
	    'description' => t('LBL_DESCRIPTION', 'menuType'),
	    //'status' => t('LBL_STATUS', 'category'),
	    'created_by' => t('LBL_CREATED_BY', 'menuType'),
	    'create_date' => t('LBL_CREATE_DATE', 'menuType'),
	    'updated_by' => t('LBL_UPDATED_BY', 'menuType'),
	    'update_date' => t('LBL_UPDATE_DATE', 'menuType'),
	);
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;

	$criteria->addSearchCondition('name', trim($this->name), true);

	$criteria->compare('id', $this->id);
	//$criteria->compare('name',$this->name,true);
	$criteria->compare('description', $this->description, true);
	//$criteria->compare('status',$this->status);
	$criteria->compare('created_by', $this->created_by, true);
	$criteria->compare('create_date', $this->create_date, true);
	$criteria->compare('updated_by', $this->updated_by, true);
	$criteria->compare('update_date', $this->update_date, true);

	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	    'pagination' => array(
		'pageSize' => LGHelpers::getPageSize($this->count()),
	    ),
	    'sort' => array(
		'defaultOrder' => array(
		    'id' => CSort::SORT_DESC,
		)
	    ),
	));
    }

    public function getStatus() {
	return CHtml::listData(LGHelpers::getStatusCategory(), 'id', 'name');
    }

    public function beforeSave() {
	if (parent::beforeSave()) {

	    $current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);
	    $logedInUser = User::model()->findByPk(Yii::app()->user->id);

	    if ($this->create_date == NULL) {
		$this->create_date = $current_date;
	    }

	    if ($this->created_by == NULL) {
		$this->created_by = $logedInUser->username;
	    }
	    $this->update_date = $current_date;
	    $this->updated_by = $logedInUser->username;

	    return true;
	}
	else
	    return false;
    }

    public function checkCanDelete() {

	$criteria = new CDbCriteria();
	$criteria->addCondition("menu_type_id = :menu_type_id");
	$criteria->params = array(':menu_type_id' => $this->id);

	return Menu::model()->count($criteria) > 0 ? false : true;
    }

    public static function checkCanDeleteMenuType($id) {

	if (isset($id)) {
	    $criteria = new CDbCriteria();
	    $criteria->addCondition("menu_type_id = :menu_type_id");
	    $criteria->params = array(':menu_type_id' => $id);
	    $menu = Menu::model()->findAll($criteria);

	    return count($menu) > 0 ? false : true;
	}

	return false;
    }

    protected function afterFind() {
	$this->update_date = strtotime($this->update_date);
	$this->update_date = date(LGConstant::DATE_FORMAT, $this->update_date);
	parent::afterFind();
    }

}